package com.qingxin.park.action;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.park.entity.Crops;
import com.qingxin.park.entity.Cropsyz;
import com.qingxin.park.entity.ProductionLink;
import com.qingxin.park.entity.ProductionyzLink;
import com.qingxin.park.service.CropsService;
import com.qingxin.park.service.CropsyzService;
import com.qingxin.park.service.ProductionLinkService;
import com.qingxin.park.service.ProductionyzLinkService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.utils.StringUtils;


@Controller
@RequestMapping("/prolink_yz")
public class ProductionyzLinkAction extends BaseAction {

	@Autowired
	private ProductionyzLinkService productionyzLinkService;
	@Autowired
	private CropsyzService cropsyzService;
	
	
	/**
	 * 获取农作物生产环节数据
	 * @param cropid
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/getProlinkData.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getProlinkData(String cropid, Integer type){
		return productionyzLinkService.getProlinkData(cropid, type);
	}
	
	/**
	 * 农作物生产环节列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "/trace/cropsyz/prolink_list";
	}
	
	/**
	 * 列表分页数据
	 * @param leftdata
	 * @param linkName
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String leftdata, String linkName, PageParam pageParam, HttpServletRequest request){
		Pager pager = productionyzLinkService.list(leftdata, linkName, pageParam, getLoginUser(request));
		return pager;
	}
	
	
	
	/**
	 * 添加农作物生产环节信息
	 * @param model
	 * @param leftdata
	 * @param datatype
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do" , method=RequestMethod.GET)
	public String add(ModelMap model, String leftdata, String datatype, HttpServletRequest request){
		if(!"CROP".equals(datatype) || StringUtils.isEmpty(leftdata)){
			model.put("errormessage", "参数错误");
			return "sys/error/error";
		}
		model.put("cropid", leftdata);
		
		Cropsyz crops = cropsyzService.get(leftdata);
		model.put("cropsyz", crops);
		
		return "trace/cropsyz/prolink_add";
	}
	
	
	/**
	 * 保存农作物生产环节信息
	 * @param link
	 * @param result
	 * @param cropid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST})
	@ResponseBody
	public Object save(@Valid ProductionyzLink link, BindingResult result, String cropid, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = productionyzLinkService.add(link, cropid, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 修改农作物生产环节信息
	 * @param model
	 * @param linkid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do" , method=RequestMethod.GET)
	public String edit(ModelMap model, String linkid, HttpServletRequest request){
		ProductionyzLink link = productionyzLinkService.get(linkid);
		model.put("link", link);
		
		Cropsyz crops = cropsyzService.get(link.getCrops().getCropid());
		model.put("cropsyz", crops);
		
		return "trace/cropsyz/prolink_edit";
	}
	
	
	/**
	 * 保存修改的信息
	 * @param link
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method={RequestMethod.POST})
	@ResponseBody
	public Object update(@Valid  ProductionyzLink link, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = productionyzLinkService.update(link, getLoginUser(request), request);
		return info;
	}
	
	
	
	/**
	 * 查看农作物生产环节详情
	 * @param model
	 * @param linkid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/view.do" , method=RequestMethod.GET)
	public String view(ModelMap model, String linkid, HttpServletRequest request){
		ProductionyzLink link = productionyzLinkService.get(linkid);
		model.put("link", link);
		
		Cropsyz crops = cropsyzService.get(link.getCrops().getCropid());
		model.put("cropsyz", crops);
		
		return "trace/cropsyz/prolink_view";
	}
	
	
	/**
	 * 删除农作物生产环节信息
	 * @param linkid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] linkid, HttpServletRequest request){
		if(linkid==null || linkid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return productionyzLinkService.delete(linkid, getLoginUser(request), request);
	}
	
	
	
	
}
